package lv.dysnomia.songs.server.controllers

import lv.dysnomia.server.entities.LilypondDAO
import lv.dysnomia.songs.server.wiki.WikiProc
import lv.dysnomia.songs.server.wiki.WikiParser
import lv.dysnomia.server.entities.SongDAO
import lv.dysnomia.server.entities.Lilypond
import com.google.appengine.api.datastore.Text
import java.util.Date
import lv.dysnomia.server.entities.Song

object SongController {

  def saveSong(title: String, lyrics: String, userId: String, label: String, lilytext:String,
    pngLink: String, svgLink: String, midiLink: String, oggLink: String): (Map[String, AnyRef], String) = {
    if (label.equals("")) {
      var s: Song = new Song
      s.setCreated(new Date)
      s.setUserId(userId)
      val label = mkLabel(title)
      s.setLabel(label)
      s.setTitle(title)
      s.setVersion(1)
      s.setNotes(new Text(""))
      s.setLyrics(new Text(lyrics))
//      val lilypond = new Lilypond
//      lilypond.setLilytext(new Text("empty"))
//      s.setLilypond(lilypond)
      s.setPngLink(pngLink)
      s.setSvgLink(svgLink)
      s.setMidiLink(midiLink)
      s.setOggLink(oggLink)

      var lily: Lilypond = new Lilypond
      lily.setLabel(label)
      lily.setLilytext(new Text(lilytext))
//      LilypondDAO.save(lily)
      s.setLilypond(lily)
      
      SongDAO.save(s)


      val s1: Song = SongDAO.getSongByLabel(label)
      (Map("song" -> s1), "save-successful")
    } else {
      var s: Song = SongDAO.getSongByLabel(label)
      s.setVersion(s.getVersion + 1)
      s.setTitle(title)
      s.setLyrics(new Text(lyrics))

//      var lily: Lilypond = LilypondDAO.getLilypondByLabel(label)
//      lily.setLilytext(new Text("lilypond"))
      s.getLilypond.setLilytext(new Text(lilytext))

      s.setPngLink(pngLink)
      s.setSvgLink(svgLink)
      s.setMidiLink(midiLink)
      s.setOggLink(oggLink)
      SongDAO.save(s)
//      LilypondDAO.save(lily)
      val s1: Song = SongDAO.getSongByLabel(label)
      val lyricsText = s1.getLyrics
      //val lyricsString = lyricsText.getValue
      val lyricsString = lyrics
      val exprs = WikiParser.parse(lyricsString)
      val xmlCode = WikiProc.eval(exprs)
      (Map("song" -> s1, "lyrics" -> xmlCode), "save-successful")
    }
  }

  /**
   * Create a plain ASCII label for the song from its title. 
   */
  def mkLabel(title: String): String = {
    val words = title.split("""[ ,:;\.\?!\-\(\)\]\[]""").filter(s => !s.equals(""))
    val asciiWords: Array[String] = words.map(word => {
      val chars = word.toLowerCase.toList.map(c => {
        c match {
          case 'ā' => "a-"
          case 'č' => "ch"
          case 'ē' => "e-"
          case 'ģ' => "g-"
          case 'ī' => "i-"
          case 'ķ' => "k-"
          case 'ļ' => "l-"
          case 'ņ' => "n-"
          case 'ō' => "o-"
          case 'š' => "sh"
          case 'ū' => "u-"
          case 'ž' => "zh"
          case 'ä' => "ae"
          case 'ö' => "oe"
          case 'ü' => "ue"
          case 'ą' => "a,"
          case 'ę' => "e,"
          case 'ė' => "e."
          case 'į' => "i,"
          case 'ų' => "u,"
          case _ => "" + c
        }

      })
      chars.mkString: String
    })
    val capWords = asciiWords.map(w =>
      w.substring(0, 1).toUpperCase + w.substring(1))
    capWords.mkString("")
  }

}